package com.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;

import com.entity.DaibantixingEntity;
import com.entity.UsersEntity;
import com.entity.YonghuEntity;
import com.service.DaibantixingService;
import com.service.UsersService;
import com.service.YonghuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;

import com.entity.GongwenxinxiEntity;

import com.service.GongwenxinxiService;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.MPUtil;

/**
 * 公文信息
 * 后端接口
 * @author 
 * @email 
 * @date 2023-03-05 15:45:54
 */
@RestController
@RequestMapping("/gongwenxinxi")
public class GongwenxinxiController {
    @Autowired
    private GongwenxinxiService gongwenxinxiService;

	@Autowired
	private DaibantixingService daibantixingService;

	@Autowired
	private UsersService userService;

	@Autowired
	private YonghuService yonghuService;

    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params,GongwenxinxiEntity gongwenxinxi,
		HttpServletRequest request){
		String tableName = request.getSession().getAttribute("tableName").toString();
		if(tableName.equals("yonghu")) {
			gongwenxinxi.setYonghuzhanghao((String)request.getSession().getAttribute("username"));
		}
        EntityWrapper<GongwenxinxiEntity> ew = new EntityWrapper<GongwenxinxiEntity>();

		PageUtils page = gongwenxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, gongwenxinxi), params), params));

        return R.ok().put("data", page);
    }
	
    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        GongwenxinxiEntity gongwenxinxi = gongwenxinxiService.selectById(id);
        return R.ok().put("data", gongwenxinxi);
    }

    /**
     * 后端保存
     */
    @RequestMapping("/save")
	@Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody GongwenxinxiEntity gongwenxinxi, HttpServletRequest request){
    	gongwenxinxi.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        gongwenxinxiService.insert(gongwenxinxi);

		String tableName = request.getSession().getAttribute("tableName").toString();
		String username = (String)request.getSession().getAttribute("username");
		String fullName = null;
		if(tableName.equals("users")) {
			UsersEntity userEntity = userService.selectOne(new EntityWrapper<UsersEntity>().eq("username", username));
			fullName = userEntity.getRole();
		}else{
			YonghuEntity yonghuEntity = yonghuService.selectOne(new EntityWrapper<YonghuEntity>().eq("yonghuzhanghao", username));
			fullName = yonghuEntity.getYonghuxingming();
		}
		DaibantixingEntity daibantixing = new DaibantixingEntity();
		daibantixing.setYonghuzhanghao(gongwenxinxi.getYonghuzhanghao());
		daibantixing.setYonghuxingming(gongwenxinxi.getYonghuxingming());
		daibantixing.setAddtime(new Date());
		daibantixing.setBiaoti(fullName + "新增公文信息提醒");
		daibantixing.setTixingneirong(gongwenxinxi.getGongwenbiaoti());
		daibantixing.setTixingshijian(daibantixing.getAddtime());
		daibantixingService.insert(daibantixing);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional(rollbackFor = Exception.class)
    public R update(@RequestBody GongwenxinxiEntity gongwenxinxi, HttpServletRequest request){
        gongwenxinxiService.updateById(gongwenxinxi);//全部更新

		String tableName = request.getSession().getAttribute("tableName").toString();
		String username = (String)request.getSession().getAttribute("username");
		String fullName = null;
		if(tableName.equals("users")) {
			UsersEntity userEntity = userService.selectOne(new EntityWrapper<UsersEntity>().eq("username", username));
			fullName = userEntity.getRole();
		}else{
			YonghuEntity yonghuEntity = yonghuService.selectOne(new EntityWrapper<YonghuEntity>().eq("yonghuzhanghao", username));
			fullName = yonghuEntity.getYonghuxingming();
		}
		DaibantixingEntity daibantixing = new DaibantixingEntity();
		daibantixing.setYonghuzhanghao(gongwenxinxi.getYonghuzhanghao());
		daibantixing.setYonghuxingming(gongwenxinxi.getYonghuxingming());
		daibantixing.setAddtime(new Date());
		daibantixing.setBiaoti(fullName + "修改公文信息提醒");
		daibantixing.setTixingneirong(gongwenxinxi.getGongwenbiaoti());
		daibantixing.setTixingshijian(daibantixing.getAddtime());
		daibantixingService.insert(daibantixing);
        return R.ok();
    }

	/**
	 * 审核修改
	 */
	@RequestMapping("/shupdate")
	@Transactional(rollbackFor = Exception.class)
	public R shupdate(@RequestBody GongwenxinxiEntity gongwenxinxi, HttpServletRequest request){
		gongwenxinxiService.updateById(gongwenxinxi);//全部更新

		String tableName = request.getSession().getAttribute("tableName").toString();
		String username = (String)request.getSession().getAttribute("username");
		String fullName = null;
		if(tableName.equals("users")) {
			UsersEntity userEntity = userService.selectOne(new EntityWrapper<UsersEntity>().eq("username", username));
			fullName = userEntity.getRole();
		}else{
			YonghuEntity yonghuEntity = yonghuService.selectOne(new EntityWrapper<YonghuEntity>().eq("yonghuzhanghao", username));
			fullName = yonghuEntity.getYonghuxingming();
		}
		DaibantixingEntity daibantixing = new DaibantixingEntity();
		daibantixing.setYonghuzhanghao(gongwenxinxi.getYonghuzhanghao());
		daibantixing.setYonghuxingming(gongwenxinxi.getYonghuxingming());
		daibantixing.setAddtime(new Date());
		daibantixing.setBiaoti(fullName + "审核完成");
		daibantixing.setTixingneirong(gongwenxinxi.getGongwenbiaoti());
		daibantixing.setTixingshijian(daibantixing.getAddtime());
		daibantixingService.insert(daibantixing);
		return R.ok();
	}

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        gongwenxinxiService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }
    
    /**
     * 提醒接口
     */
	@RequestMapping("/remind/{columnName}/{type}")
	public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request, 
						 @PathVariable("type") String type,@RequestParam Map<String, Object> map) {
		map.put("column", columnName);
		map.put("type", type);
		
		if(type.equals("2")) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			Date remindStartDate = null;
			Date remindEndDate = null;
			if(map.get("remindstart")!=null) {
				Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
				c.setTime(new Date()); 
				c.add(Calendar.DAY_OF_MONTH,remindStart);
				remindStartDate = c.getTime();
				map.put("remindstart", sdf.format(remindStartDate));
			}
			if(map.get("remindend")!=null) {
				Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
				c.setTime(new Date());
				c.add(Calendar.DAY_OF_MONTH,remindEnd);
				remindEndDate = c.getTime();
				map.put("remindend", sdf.format(remindEndDate));
			}
		}
		
		Wrapper<GongwenxinxiEntity> wrapper = new EntityWrapper<GongwenxinxiEntity>();
		if(map.get("remindstart")!=null) {
			wrapper.ge(columnName, map.get("remindstart"));
		}
		if(map.get("remindend")!=null) {
			wrapper.le(columnName, map.get("remindend"));
		}

		String tableName = request.getSession().getAttribute("tableName").toString();
		if(tableName.equals("yonghu")) {
			wrapper.eq("yonghuzhanghao", (String)request.getSession().getAttribute("username"));
		}

		int count = gongwenxinxiService.selectCount(wrapper);
		return R.ok().put("count", count);
	}
	









}
